package com.niit.service.impl;

import   com.niit.entity.Application;
import com.niit.entity.Job;
import com.niit.entity.User;
import com.niit.repository.ApplicationRepository;
import com.niit.service.ApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ApplicationServiceImpl implements ApplicationService {
    @Autowired
    private ApplicationRepository applicationRepository;


    @Override
    public void addApplication(Application application) {
        applicationRepository.save(application);
    }

    @Override
    public void deleteApplication(int id) {
        applicationRepository.deleteById(id);
    }

    @Override
    public void updateApplication(Application application) {
        applicationRepository.save(application);
    }

    @Override
    public List<Application> getApplications() {
        return applicationRepository.findAll();
    }

    @Override
    public Application getApplicationById(int id) {
        return applicationRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("未找到该申请"));
    }

    @Override
    public List<Application> getSeekerApplications(User seeker) {
        return applicationRepository.findBySeeker(seeker);
    }

    @Override
    public List<Application> getJobApplications(Job job) {
        return applicationRepository.findByJob(job);
    }
    //根据职位发布者查找所有申请
    @Override
    public List<Application> getCompanyApplications(User user) {
        return applicationRepository.findByJobPoster(user);
    }

    @Override
    public Long getCountByJob(Job job) {
        return applicationRepository.countByJob(job);
    }

    @Override
    public boolean existsByJobIdAndSeekerId(Integer jobId, Integer seekerId) {
        return applicationRepository.existsByJobIdAndSeekerId(jobId, seekerId);
    }

}
